package com.example.yoho.mvp.presenter;

import com.example.lib_core.mvp.presenter.BasePresenter;
import com.example.yoho.mvp.contract.LoginContract;
import com.example.yoho.mvp.model.entity.LoginEntity;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<LoginContract.Model,LoginContract.View> {
    String name;
    String pwd;

    @Inject
    public LoginPresenter() {

    }

    public void loginAll(String name,String pwd){
        this.name=name;
        this.pwd=pwd;

        JSONObject loginJob = new JSONObject();
        try {

            loginJob.put("username",name);
            loginJob.put("password",pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mModel.loginModel(loginJob.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);
                        mView.showDialog();
                    }

                    @Override
                    public void onNext(LoginEntity loginEntity) {
                        mView.showLogin(loginEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showToast("请求失败"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
